package cocoaas3.nd2d{

	import jp.progression.casts.*;
	import jp.progression.commands.display.*;
	import jp.progression.commands.lists.*;
	import jp.progression.commands.managers.*;
	import jp.progression.commands.media.*;
	import jp.progression.commands.net.*;
	import jp.progression.commands.tweens.*;
	import jp.progression.commands.*;
	import jp.progression.data.*;
	import jp.progression.events.*;
	import jp.progression.scenes.*;

	import flash.text.*;
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;

	import cocoaas3.*;
	import jp.dividual.nativeExtensions.*;



	public class UINavigationBar extends UIView {

		// original
		var items:Array = new Array();

		// custom
		public const DEFAULT_TWEEN_TIME:Number = 0.6;
		//var view = new UINavigationBar_view();
		private var itemContainer_mc:Sprite = new Sprite(); 

		public function UINavigationBar(){
			super();
			//addChild( view );
			//addChild( itemContainer_mc );
		}


		//
		// original methods
		//

		// レシーバのスタックにアイテムを追加してナビゲーションバーを更新する。
		public function pushNavigationItem( item:UINavigationItem, animated:Boolean=true ):void{
			var stage:Stage = UIApplication.sharedApplication().stage;


			items.push( item );

			var prevItem_mc:Sprite 
			if( itemContainer_mc.numChildren > 0 ){
				prevItem_mc = itemContainer_mc.getChildAt( 0 ) as Sprite;
			}

			// 戻るボタンの表示設定
			if( items.length == 1 ){
				item.hidesBackButton = true;
			}

			// UINavigationItem から Sprite を作成
			var navItem_mc:Sprite = _createSpriteOf( item );

			var list = new SerialList();

			list.addCommand( new AddChild( itemContainer_mc, navItem_mc ) );
			if( animated ){
				navItem_mc.x = stage.width;
				var animeList = new ParallelList();
				if( prevItem_mc ){
					animeList.addCommand( new DoTweener( prevItem_mc, {x:0-stage.width, time:DEFAULT_TWEEN_TIME, transition:"easeInOutQuart"} ) );
				}
				animeList.addCommand( new DoTweener( navItem_mc, {x:0, time:DEFAULT_TWEEN_TIME, transition:"easeInOutQuart"} ) );
				list.addCommand( animeList );
			} else {
				if( prevItem_mc ){
					list.addCommand( new RemoveChild( itemContainer_mc, prevItem_mc ) );
				}
			}
			list.execute();
		}

		// レシーバのスタックの最上位のアイテムを削除してナビゲーションバーを更新する。
		public function popNavigationItem( animated:Boolean=true ):UINavigationItem{
			var stage:Stage = UIApplication.sharedApplication().stage;


			var item:UINavigationItem = items.pop();
			var prevItem:UINavigationItem = items[items.length-1];

			var list = new SerialList();
			var time_num:Number = DEFAULT_TWEEN_TIME;
			if( animated==false ){
				time_num = 0;
			}
			list.addCommand(
				[
					new DoTweener( item, {x:stage.stageWidth, time:time_num, transition:"easeInOutQuart"} ),
					new DoTweener( prevItem, {x:0, time:time_num, transition:"easeInOutQuart"} ),
				],
				new RemoveChild( itemContainer_mc, item ),
			null);
			list.execute();
			return item;
		}




		//
		//
		// custom
		//
		//

		// UINavigationItem から Sprite を作成
		private function _createSpriteOf( navItem:UINavigationItem ):Sprite{
			var sprite:Sprite = new Sprite();
			var tf:TextField = new TextField();
			tf.text = navItem.title;
			sprite.addChild( tf );
			return sprite;
		}

	}
}